﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace AutoDeploy.FrontEnd
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class EachStep : Window
    {
        public ObservableCollection<AutoDeploy.Common.Library.CommandValuesView> ocCVV;
        private StringBuilder sb = new StringBuilder();
        private int CurrentStep = 0;
        private AutoDeploy.Common.Library.DeploymentObject ado;
        private ObservableCollection<Common.Library.CommandValuesView> oc;
        private bool hasStartMessageRun = false;
        /// <summary>
        /// Loads the EachStep form
        /// </summary>
        /// <param name="oc"></param>
        /// <param name="deploymentObject"></param>
        public EachStep(ObservableCollection<Common.Library.CommandValuesView> oc, AutoDeploy.Common.Library.DeploymentObject deploymentObject)
        {
            try
            {
                InitializeComponent();
                ado = deploymentObject;
                this.Title = "Auto Deployer-" + ado.Name;
                this.oc = oc;
                butRun.IsEnabled = true;
                NextStep();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Problem loading the ado {0}",ex.Message));
            }
        }
        /// <summary>
        /// Sets Next Step
        /// </summary>
        public void NextStep()
        {
            try
            {
                if (CurrentStep > 0)
                {
                    if (CurrentStep <= ado.CommandSets.Count)
                    {
                        SetStepFormProperties();
                    }
                    else
                    {
                        EndMessage();
                    }
                }
                else
                {
                    if (!hasStartMessageRun)
                    {
                        StartMessage();
                    }
                    SetStepFormProperties();
                }
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem loading the ado" + ex.Message);
            }
        }
        /// <summary>
        /// EndMessage
        /// </summary>
        private void EndMessage()
        {
            try
            {
                string s = "Email MarkWRowe@hotmail.com or post on autodeploy.codeplex.com if there are any problems or feature requests.\n";
                MessageBox.Show(s);
                sb.Append(s);
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem with EndMessage" + ex.Message);
            }
        }
        /// <summary>
        /// StartMessage
        /// </summary>
        private void StartMessage()
        {
            try
            {
                string s = "Thank you for using Auto Deploy!\nPlease Check all of the commands before they are run.\n" +
                    "This program allows for XML configuration file changes, Shell Processes,  Run Database Commands and Scripts, \n" +
                    "It can also check installed programs and services and registry entries to be installed.\n" +
                    "You choose your AutoDeploy.xml file to run, so please just review the commands run.\n" +
                    "If you have not reviewed the AutoDeploy PLEASE TERMINATE and review the commands.\n" +
                    "Check autodeploy.codeplex.com for updates. Thank you, Mark Rowe \n";
                MessageBox.Show(s);
                sb.Append(s);
                hasStartMessageRun = true;
                butRunAll.IsEnabled = (ado.FastDeploy);
                CurrentStep++;
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem loading the ado" + ex.Message);
            }

        }
        /// <summary>
        ///  LoadDataGrid
        /// </summary>
        private void LoadDataGrid()
        {
            try
            {
                ocCVV = new ObservableCollection<AutoDeploy.Common.Library.CommandValuesView>();
                if (ado.CommandSets.Count >= CurrentStep)
                {
                    foreach (var v in oc.Where(x => x.Priority == CurrentStep))
                    {
                        ocCVV.Add(v);
                    }
                    dgCommands.ItemsSource = ocCVV;
                }
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem loading the DataGrid:{0}", ex.Message));
            }
        }
        /// <summary>
        /// SetStepFormProperties
        /// </summary>
        private void SetStepFormProperties()
        {
            try
            {
                LoadDataGrid();
                SetButtons();
                this.Title = "Auto Deployer -" + ado.Name + " - " + (CurrentStep);
                tbStatus.Text += sb.ToString();
                sb.Clear();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem loading the DataGrid:{0}", ex.Message));
            }
        }
        /// <summary>
        /// SetButtons
        /// </summary>
        private void SetButtons()
        {
            try
            {
                butBack.IsEnabled = (CurrentStep > 1);
                butNext.IsEnabled = (CurrentStep < ado.CommandSets.Count);
                butRun.IsEnabled = RunButtonEnabled();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem Setting the buttons:{0}", ex.Message));
            }
        }
        /// <summary>
        /// Terminate button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butTerminate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(String.Format("Problem Setting the buttons:{0}", ex.Message));
            }
        }
        /// <summary>
        /// Next Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butNext_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CurrentStep++;
                NextStep();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With Next Button:{0}", ex.Message));
            }
        }
        /// <summary>
        /// Back Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butBack_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CurrentStep--;
                NextStep();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With Back Button:{0}", ex.Message));
            }
        }
        /// <summary>
        /// RunAllCommandsLeft
        /// </summary>
        private void RunAllCommandsLeft()
        {
            try
            {
                int c = CurrentStep;
                for (int i = c; i < ado.CommandSets.Count; i++)
                {
                    if (RunButtonEnabled())
                    {
                        sb.Append(AutoDeploy.Common.Library.DeployHelper.RunCurrentCommand(ado.CommandSets[CurrentStep - 1], ado.RunAsUserName, ado.RunAsPassword, ado.SQLConnectionString));
                    }
                    else
                    {
                        sb.Append(string.Format("One or more of the Run checkboxes are unchecked, AutoDeploy does not have the capability for PARTIAL Command runs."));
                    }
                    tbStatus.Text += sb.ToString();
                    SetStepFormProperties();
                    CurrentStep = i;
                }
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With RunAllCommandsLeft:{0}", ex.Message));
            }
        }
        /// <summary>
        /// Run All Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butRunAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RunAllCommandsLeft();
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With RunAllButton:{0}", ex.Message));
            }
        }
        /// <summary>
        /// Run Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butRun_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (RunButtonEnabled())
                {
                    sb.Append(AutoDeploy.Common.Library.DeployHelper.RunCurrentCommand(ado.CommandSets[CurrentStep - 1], ado.RunAsUserName, ado.RunAsPassword, ado.SQLConnectionString));
                    tbStatus.Text += sb.ToString();
                    //MessageBox.Show(sb.ToString());
                    sb.Clear();
                }
                butRun.IsEnabled = false;
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With RunAllButton:{0}", ex.Message));
            }
        }
        /// <summary>
        /// RunButtonEnabled
        /// </summary>
        /// <returns></returns>
        private bool RunButtonEnabled()
        {
            try
            {
                if (ocCVV != null)
                {
                    foreach (var item in ocCVV)
                    {
                        if (!item.Run)
                        {
                            return false;
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With RunButtonEnabled:{0}", ex.Message));
            }
            return false;
        }
        /// <summary>
        /// Sets CheckBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                butRun.IsEnabled = (RunButtonEnabled());
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Problem With CheckBox_Checked:{0}", ex.Message));
            }
        }


    }
}
